/*
 * Copyright 2011-2012 by The Regents of the University of California Licensed
 * under the Apache License, Version 2.0 (the "License"); you may not use this
 * file except in compliance with the License. you may obtain a copy of the
 * License from
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package edu.ucsd.db.bassarisk.query.path;

import java.util.logging.Logger;

import edu.ucsd.db.bassarisk.exception.VisitorException;
import edu.ucsd.db.bassarisk.query.path.DSDElements.DSDElement;
import edu.ucsd.db.bassarisk.schema.Schema.ValueType;

public class DataSequenceDefinition {
  private static final Logger LOG = Logger.getLogger(DataSequenceDefinition.class
      .getName());
  protected DSDElement head;
  private final ValueType type;

  protected DataSequenceDefinition(final DSDElement head, final ValueType type) {
    this.head = head;
    this.type = type;
  }

  public <R extends DSDElement, A> void
      applyTransformation(final DSDElementVisitor<R, A> visitor, final A args) {
    try {
      this.head = this.head.accept(visitor, args);
    } catch (VisitorException e) {
      e.printStackTrace();
      String msg = "Internal Error";
      LOG.severe(msg);
      throw new RuntimeException(msg, e);
    }
  }

  public <R, A> R applyVisitor(final DSDElementVisitor<R, A> visitor, final A args) {
    try {
      return this.head.accept(visitor, args);
    } catch (VisitorException e) {
      e.printStackTrace();
      String msg = "Internal Error";
      LOG.severe(msg);
      throw new RuntimeException(msg, e);
    }
  }

  public ValueType getType() {
    return this.type;
  }

  @Override
  public String toString() {
    return head.toString();
  }
}
